 /** All rights Reserved, Copyright (C) Aisino LIMITED 2019
 * FileName: MyRunnable.java
 * Version:  1.0
 * Modify record:
 * NO. |     Date       |    Name        |      Content
 * 1   | 2019年4月1日        | Aisino)Jack    | original version
 */
package com.jack.hhitseat.task;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jack.hhitseat.bean.AppBean;
import com.jack.hhitseat.bean.User;
import com.jack.hhitseat.service.HttpClient;
import com.jack.hhitseat.service.impl.LogServiceImpl;
import com.jack.hhitseat.utils.MyUtils;

/**
 * class name: MyRunnable <BR>
 * class description: please write your description <BR>
 * @version 1.0  2019年4月1日 上午8:57:03
 * @author Aisino)Jack
 */
//为了线程安全，不能依赖注入！！！
public class MyRunnable2 extends Thread {
	private final Logger logger = LoggerFactory.getLogger(MyRunnable2.class);
	private static HttpClient httpClient = new HttpClient();
	private static LogServiceImpl logServiceImpl = (LogServiceImpl) AppBean.getBean("logServiceImpl");
	private User u;
	private Map<String, String> sessionMap = new HashMap<>();
	private final static String KEY = "16";
	
	private final static  String BOOK_URL = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/reserve.aspx?" +
			"&dev_id={dev_id}" + "&type={type}" + "&start={start}" + "&end={end}" +
			"&start_time={start_time}" + "&end_time={end_time}"+"&number={number}" + "&act={act}" +
			"&_={_}"; 
	
	public MyRunnable2() {
	}
	
	public MyRunnable2(User u
			, Map<String, String> sessionMap) {
		this.u = u;
		this.sessionMap = sessionMap;
	}
	
	@Override
	public void run() {
		String stuNum = u.getStuNum();
		int flag = logServiceImpl.isSuccess(stuNum);
		if(flag > 0) {
			return;
		}
		String[] seats = u.getSeat().split(",");
		String session = sessionMap.get(stuNum);
		Map<String, Object> qzParparamMap = new HashMap<String, Object>();

    	String yyrq = MyUtils.getNextDate(); 
		qzParparamMap.put("type","dev");
		qzParparamMap.put("start",yyrq+"+09:30");
		qzParparamMap.put("end",yyrq+"+22:00");
		qzParparamMap.put("start_time","930"); 
		qzParparamMap.put("end_time","2200");
		qzParparamMap.put("number", KEY);			//万能钥匙Key
		qzParparamMap.put("act","set_resv");
		
		Date d = MyUtils.String2Date(yyrq);
		  
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;	//预约日期是星期几
		
		for (String str : seats) {
			final int COUNT = 300;
			String seatNumb = str.split("=")[0];
			String result = "";
			int i = 0;
			boolean exit = false;
			qzParparamMap.put("dev_id", seatNumb); 
			String sjc = (System.currentTimeMillis()+10000)+"";
			qzParparamMap.put("_",sjc);
			
			if(w != 3) {
				for (i = 1; i <= COUNT; i++) {
					result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
					if(!result.contains("验证码")) {
						if(result.contains("成功")) {
							logServiceImpl.addLog(stuNum, seatNumb);
							exit = true;
							break;
						}else if(result.contains("已有预约")) {
							exit = true;
							break;
						}
						logger.warn("【预约】==={}==={}==={}==={}", i, u.getUserName() ,
								result.split("\"msg\":\"")[1].split("\",\"data\":")[0] , str.split("=")[1]);
						break;
					}else {
						httpClient.yzm(session);
					}
				}
				if(exit) {
					break;
				}
			}else {
				for (i = 1; i <= COUNT; i++) {
					qzParparamMap.put("start",yyrq+"+09:30");
					qzParparamMap.put("start_time","930"); 
					qzParparamMap.put("end",yyrq+"+12:00");
					qzParparamMap.put("end_time","1200");
					result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
					qzParparamMap.put("start",yyrq+"+18:30");
					qzParparamMap.put("start_time","1830"); 
					qzParparamMap.put("end",yyrq+"+22:00");		
					qzParparamMap.put("end_time","2200");		
					result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
					if(!result.contains("验证码")) {
						if(result.contains("成功")) {
							logServiceImpl.addLog(stuNum, seatNumb);
							exit = true;
							break;
						}else if(result.contains("已有预约")) {
							exit = true;
							break;
						}
						logger.warn("【预约】==={}==={}==={}==={}", i, u.getUserName() ,
								result.split("\"msg\":\"")[1].split("\",\"data\":")[0] , str.split("=")[1]);
						break;
					}else {
						httpClient.yzm(session);
					}
				}
				if(exit) {
					break;
				}
			}
		}
	}
}
